//
//  StatusTableViewCell.swift
//  JLWeibo
//
//  Created by sunjinliang on 15/12/11.
//  Copyright © 2015年 sunjinliang. All rights reserved.
//

import UIKit
let statusCellMargin = 12
let statusCelliconViewWidth = 35
class StatusTableViewCell: UITableViewCell {

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    var statusView: StatusViewModel? {
        didSet{
            topView.statusView = statusView
            contentLabel.text = statusView?.status.text
            pictureView.statusViewModel = statusView
            pictureView.snp_updateConstraints { (make) -> Void in
                make.width.equalTo(pictureView.bounds.size.width)
                make.height.equalTo(pictureView.bounds.size.height)
            }
        }
    }
    /// 计算行高
    ///
    /// - parameter vm: 模型参数
    ///
    /// - returns: 返回行高
    func rowHeight(vm: StatusViewModel)->CGFloat{
        statusView = vm
        layoutIfNeeded()
        return CGRectGetMaxY(bottomView.frame)
    }
     lazy var topView: StatusCellTopView = StatusCellTopView()
     lazy var contentLabel: UILabel = {
       let label = UILabel(text1: "微博正文",  fontSize: 15)
        label.textAlignment = .Left
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - CGFloat(statusCellMargin * 2)
        return label
    }()
     lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
/// 配图
     lazy var pictureView: statusPictureView = statusPictureView()
     func setupUI(){
        
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(bottomView)
        contentView.addSubview(pictureView)
        /// 添加顶部分割线和约束
        let topDividingLine = UIView()
        topDividingLine.backgroundColor = UIColor.lightGrayColor()
        contentView.addSubview(topDividingLine)
        topDividingLine.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(statusCellMargin)
        }
        topView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(topDividingLine.snp_left)
            make.top.equalTo(topDividingLine.snp_bottom)
            make.right.equalTo(topDividingLine.snp_right)
            make.height.equalTo(statusCellMargin+statusCelliconViewWidth)
        }
            contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(statusCellMargin)
           // make.right.equalTo(contentView.snp_right).offset(-statusCellMargin)

        }
/// 配图的约束
//        pictureView.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(contentLabel.snp_bottom).offset(statusCellMargin)
//            make.left.equalTo(contentLabel.snp_left)
//            make.width.equalTo(300)
//            make.height.equalTo(90)
        
//        }
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
            //make.bottom.equalTo(contentView.snp_bottom)
        }
    }
}
